package com.unicom.room.exception;

import java.util.HashMap;
import java.util.Map;

import com.unicom.room.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyControllerAdvice {

	/**
	 * 全局捕获异常，只要作用于@RequestMapping方法上，所有的异常信息都能被捕获到
	 */
	@ResponseBody
	@ExceptionHandler(value=Exception.class)
	public Map<String,Object> errorHandler(Exception ex) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", -1);
		map.put("msg", ex.getMessage());
		return map;
	}

	/**
	 * 自定义异常
	 */
	@ResponseBody
	@ExceptionHandler(value= BusinessException.class)
	public Map<String,Object> errorHandler(BusinessException ex) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", ex.getCode());
		map.put("msg", ex.getMsg());
		return map;
	}

}
